package com.gjp.springbootexception.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.lang.Nullable;

import java.util.Objects;

/**
 * 这个类是Copy的HttpStatus类中的部分代码翻译改造的，实际项目中也可以使用
 */
@AllArgsConstructor
@Getter
public enum HttpStatusEnum {
    CONTINUE(100, "继续(Continue)"),
    SWITCHING_PROTOCOLS(101, "交换协议(Switching Protocols)"),
    PROCESSING(102, "处理中(Processing)"),
    CHECKPOINT(103, "检查点(Checkpoint)"),
    OK(200, "OK"),
    CREATED(201, "已创建(Created)"),
    ACCEPTED(202, "已接受(Accepted)"),
    NON_AUTHORITATIVE_INFORMATION(203, "非权威信息(Non-Authoritative Information)"),
    NO_CONTENT(204, "无内容(No Content)"),
    RESET_CONTENT(205, "重置内容(Reset Content)"),
    PARTIAL_CONTENT(206, "部分内容(Partial Content)"),
    MULTI_STATUS(207, "多状态(Multi-Status)"),
    ALREADY_REPORTED(208, "已经报道过了(Already Reported)"),
    IM_USED(226, "IM 已使用(IM Used)"),
    MULTIPLE_CHOICES(300, "多种选择(Multiple Choices)"),
    MOVED_PERMANENTLY(301, "永久转移(Moved Permanently)"),
    FOUND(302, "发现(Found)"),
    /** @deprecated */
    @Deprecated
    MOVED_TEMPORARILY(302, "暂时移动(Moved Temporarily)"),
    SEE_OTHER(303, "参见其他(See Other)"),
    NOT_MODIFIED(304, "未修改(Not Modified)"),
    /** @deprecated */
    @Deprecated
    USE_PROXY(305, "使用代理服务器(Use Proxy)"),
    TEMPORARY_REDIRECT(307, "临时重定向(Temporary Redirect)"),
    PERMANENT_REDIRECT(308, "永久重定向(Permanent Redirect)"),
    BAD_REQUEST(400, "错误的请求(Bad Request)"),
    UNAUTHORIZED(401, "未经授权(Unauthorized)"),
    PAYMENT_REQUIRED(402, "付费请求(Payment Required)"),
    FORBIDDEN(403, "被禁止(Forbidden)"),
    NOT_FOUND(404, "未找到(Not Found)"),
    METHOD_NOT_ALLOWED(405, "方法不允许(Method Not Allowed)"),
    NOT_ACCEPTABLE(406, "不可接受(Not Acceptable)"),
    PROXY_AUTHENTICATION_REQUIRED(407, "需要代理身份验证(Proxy Authentication Required)"),
    REQUEST_TIMEOUT(408, "请求超时(Request Timeout)"),
    CONFLICT(409, "指令冲突(Conflict)"),
    GONE(410, "文档永久地离开了指定的位置(Gone)"),
    LENGTH_REQUIRED(411, "需要 Content-Length 头请求(Length Required)"),
    PRECONDITION_FAILED(412, "前提条件失败(Precondition Failed)"),
    PAYLOAD_TOO_LARGE(413, "请求实体太大(Payload Too Large)"),
    /** @deprecated */
    @Deprecated
    REQUEST_ENTITY_TOO_LARGE(413, "请求实体太大(Request Entity Too Large)"),
    URI_TOO_LONG(414, "请求URI太长(URI Too Long)"),
    /** @deprecated */
    @Deprecated
    REQUEST_URI_TOO_LONG(414, "请求URI太长(Request-URI Too Long)"),
    UNSUPPORTED_MEDIA_TYPE(415, "不支持的媒体类型(Unsupported Media Type)"),
    REQUESTED_RANGE_NOT_SATISFIABLE(416, "请求的范围不可满足(Requested range not satisfiable)"),
    EXPECTATION_FAILED(417, "期望失败(Expectation Failed)"),
    I_AM_A_TEAPOT(418, "我是一个茶壶(彩蛋)(I'm a teapot)"),
    /** @deprecated */
    @Deprecated
    INSUFFICIENT_SPACE_ON_RESOURCE(419, "资源空间不足(Insufficient Space On Resource)"),
    /** @deprecated */
    @Deprecated
    METHOD_FAILURE(420, "方法错误(Method Failure)"),
    /** @deprecated */
    @Deprecated
    DESTINATION_LOCKED(421, "目的地已锁定(Destination Locked)"),
    UNPROCESSABLE_ENTITY(422, "不可处理的实体(Unprocessable Entity)"),
    LOCKED(423, "已锁定(Locked)"),
    FAILED_DEPENDENCY(424, "错误的依赖(Failed Dependency)"),
    UPGRADE_REQUIRED(426, "需要 Upgrade 头请求(Upgrade Required)"),
    PRECONDITION_REQUIRED(428, "需要 Precondition 头请求(Precondition Required)"),
    TOO_MANY_REQUESTS(429, "请求过多(Too Many Requests)"),
    REQUEST_HEADER_FIELDS_TOO_LARGE(431, "请求标头字段太大(Request Header Fields Too Large)"),
    UNAVAILABLE_FOR_LEGAL_REASONS(451, "法律原因不可用(Unavailable For Legal Reasons)"),
    INTERNAL_SERVER_ERROR(500, "服务器内部错误(Internal Server Error)"),
    NOT_IMPLEMENTED(501, "未实现(Not Implemented)"),
    BAD_GATEWAY(502, "错误的网关(Bad Gateway)"),
    SERVICE_UNAVAILABLE(503, "暂停服务(Service Unavailable)"),
    GATEWAY_TIMEOUT(504, "网关超时(Gateway Timeout)"),
    HTTP_VERSION_NOT_SUPPORTED(505, "HTTP版本不支持(HTTP Version not supported)"),
    VARIANT_ALSO_NEGOTIATES(506, "变种谈判中(Variant Also Negotiates)"),
    INSUFFICIENT_STORAGE(507, "存储空间不足(Insufficient Storage)"),
    LOOP_DETECTED(508, "检测到环路(Loop Detected)"),
    BANDWIDTH_LIMIT_EXCEEDED(509, "超出带宽限制(Bandwidth Limit Exceeded)"),
    NOT_EXTENDED(510, "未扩展(Not Extended)"),
    NETWORK_AUTHENTICATION_REQUIRED(511, "需要网络验证(Network Authentication Required)"),
    PARAM_ERROR(1,"参数错误()");

    private final int code;
    private final String reason;

    public static HttpStatusEnum valueOf(int code) {
        HttpStatusEnum httpStatusEnum = resolve(code);
        if (Objects.isNull(httpStatusEnum)) {
            throw new IllegalArgumentException("无法匹配到该状态[" + code + "]");
        } else {
            return httpStatusEnum;
        }
    }

    @Nullable
    public static HttpStatusEnum resolve(int statusCode) {
        for (HttpStatusEnum httpStatusEnum : values()) {
            if (httpStatusEnum.getCode() == statusCode){
                return httpStatusEnum;
            }
        }
        return null;
    }
}
